//给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。 
//
// 
//
// 示例： 
//二叉树：[3,9,20,null,null,15,7], 
//
// 
//    3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回其层序遍历结果： 
//
// 
//[
//  [3],
//  [9,20],
//  [15,7]
//]
// 
// Related Topics 树 广度优先搜索 二叉树 👍 1132 👎 0

package leetcode.editor.cn;

import java.util.*;

public class _102_BinaryTreeLevelOrderTraversal {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public static void main(String[] args) {
        int n = Integer.MAX_VALUE;
        int[] nums = new int[]{3, 9, 20, n, n, 15, 7, n, n, n, n, n, n, n, n};
//        int[] nums = new int[]{1, 2, 3, n, n, n, n};
        TreeNode root = creatTree(nums, 0);
        Solution solution = new _102_BinaryTreeLevelOrderTraversal().new Solution();
        List<List<Integer>> lists = solution.levelOrder(root);
        for (List<Integer> list : lists) {
            for (Integer num : list) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }

    /**
     * 层序创建二叉树
     */
    private static TreeNode creatTree(int[] nums, int index) {
        if (nums[index] == Integer.MAX_VALUE) {
            return null;
        }
        TreeNode root = new TreeNode(nums[index]);
        root.left = creatTree(nums, 2 * index + 1);
        root.right = creatTree(nums, 2 * index + 2);
        return root;
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        /**
         * 用队列实现广度优先遍历
         * @param root
         * @return
         */
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            Queue<TreeNode> q = new LinkedList<>();
            TreeNode dummy = new TreeNode();

            if (root == null) {
                return res;
            }

            q.add(root);
            q.add(dummy);
            while (!q.isEmpty() && q.peek() != dummy) {
                List<Integer> levelRes = new ArrayList<>();
                while (q.peek() != dummy) {
                    TreeNode tmp = q.poll();
                    levelRes.add(tmp.val);
                    if (tmp.left != null) q.add(tmp.left);
                    if (tmp.right != null) q.add(tmp.right);
                }
                q.poll();
                q.add(dummy);
                res.add(levelRes);
            }
            return res;
        }

        List<List<Integer>> levels = new ArrayList<List<Integer>>();
        /**
         * 官方解法，递归
         * @param root
         * @return
         */
        public List<List<Integer>> levelOrder_digui(TreeNode root) {
            if (root == null) {
                return levels;
            }
            helper(root, 0);
            return levels;
        }

        private void helper(TreeNode root, int level) {
            if (levels.size() == level) {
                levels.add(new ArrayList<Integer>());
            }

            levels.get(level).add(root.val);

            if (root.left != null) {
                helper(root.left, level + 1);
            }
            if (root.right != null) {
                helper(root.right, level + 1);
            }
        }


    }
//leetcode submit region end(Prohibit modification and deletion)

}